/*     */ package com.zimbra.cs.account;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import java.security.SecureRandom;
/*     */ import java.util.HashMap;
/*     */ import org.apache.commons.codec.DecoderException;
/*     */ import org.apache.commons.codec.binary.Hex;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class AuthTokenKey
/*     */ {
/*     */   public static final int KEY_SIZE_BYTES = 32;
/*     */   private byte[] mKey;
/*     */   private long mVersion;
/*     */   private long mCreated;
/*  43 */   private static HashMap<String, AuthTokenKey> mCache = new HashMap();
/*     */   private static AuthTokenKey sLatestKey;
/*     */   
/*     */   public byte[] getKey() {
/*  47 */     return this.mKey;
/*     */   }
/*     */   
/*     */   public long getVersion() {
/*  51 */     return this.mVersion;
/*     */   }
/*     */   
/*     */   public long getCreated() {
/*  55 */     return this.mCreated;
/*     */   }
/*     */   
/*     */   void setKey(byte[] key) {
/*  59 */     this.mKey = key;
/*     */   }
/*     */   
/*     */   AuthTokenKey(long version, byte[] key) throws ServiceException {
/*  63 */     this.mVersion = version;
/*  64 */     this.mCreated = System.currentTimeMillis();
/*  65 */     if (key != null) {
/*  66 */       this.mKey = key;
/*     */     } else {
/*  68 */       SecureRandom random = new SecureRandom();
/*  69 */       this.mKey = new byte[32];
/*  70 */       random.nextBytes(this.mKey);
/*     */     }
/*     */   }
/*     */   
/*     */   private AuthTokenKey(String k) throws ServiceException {
/*  75 */     String[] parts = k.split(":");
/*  76 */     if (parts.length != 3)
/*  77 */       throw ServiceException.INVALID_REQUEST("invalid auth token key", null);
/*  78 */     String ver = parts[0];
/*  79 */     String created = parts[1];
/*  80 */     String data = parts[2];
/*     */     try
/*     */     {
/*  83 */       this.mVersion = Long.parseLong(ver);
/*     */     } catch (NumberFormatException e) {
/*  85 */       throw ServiceException.INVALID_REQUEST("invalid auth token key version", e);
/*     */     }
/*     */     try
/*     */     {
/*  89 */       this.mCreated = Long.parseLong(created);
/*     */     } catch (NumberFormatException e) {
/*  91 */       throw ServiceException.INVALID_REQUEST("invalid auth token key created data", e);
/*     */     }
/*     */     try
/*     */     {
/*  95 */       this.mKey = Hex.decodeHex(data.toCharArray());
/*     */     } catch (DecoderException e) {
/*  97 */       throw ServiceException.INVALID_REQUEST("invalid auth token key data", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public String getEncoded() {
/* 102 */     return this.mVersion + ":" + this.mCreated + ":" + new String(Hex.encodeHex(this.mKey));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static AuthTokenKey getVersion(String version)
/*     */     throws ServiceException
/*     */   {
/* 112 */     AuthTokenKey key = (AuthTokenKey)mCache.get(version);
/*     */     
/*     */ 
/* 115 */     if (key == null) {
/* 116 */       refresh(false);
/*     */     }
/* 118 */     key = (AuthTokenKey)mCache.get(version);
/*     */     
/*     */ 
/* 121 */     if (key == null)
/* 122 */       refresh(true);
/* 123 */     key = (AuthTokenKey)mCache.get(version);
/*     */     
/*     */ 
/* 126 */     return key;
/*     */   }
/*     */   
/*     */   private static synchronized void refresh(boolean reload) throws ServiceException {
/* 130 */     Provisioning prov = Provisioning.getInstance();
/* 131 */     Config config = prov.getConfig();
/*     */     
/* 133 */     if (reload) {
/* 134 */       prov.reload(config);
/*     */     }
/* 136 */     String[] keys = config.getMultiAttr("zimbraAuthTokenKey");
/*     */     
/* 138 */     if (keys.length == 0) {
/* 139 */       prov.reload(config);
/* 140 */       keys = config.getMultiAttr("zimbraAuthTokenKey");
/*     */     }
/*     */     
/*     */ 
/* 144 */     if (keys.length == 0) {
/* 145 */       AuthTokenKey key = new AuthTokenKey(0L, null);
/* 146 */       HashMap<String, String> attrs = new HashMap();
/* 147 */       attrs.put("zimbraAuthTokenKey", key.getEncoded());
/* 148 */       Provisioning.getInstance().modifyAttrs(config, attrs);
/* 149 */       keys = config.getMultiAttr("zimbraAuthTokenKey");
/*     */     }
/*     */     
/* 152 */     for (int i = 0; i < keys.length; i++) {
/* 153 */       AuthTokenKey key = (AuthTokenKey)mCache.get(keys[i]);
/* 154 */       if (key == null) {
/* 155 */         key = new AuthTokenKey(keys[i]);
/* 156 */         mCache.put(keys[i], key);
/* 157 */         mCache.put(Long.toString(key.mVersion), key);
/* 158 */         if ((sLatestKey == null) || (sLatestKey.mVersion < key.mVersion))
/* 159 */           sLatestKey = key;
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static synchronized AuthTokenKey getCurrentKey() throws ServiceException {
/* 165 */     if (sLatestKey == null) {
/* 166 */       refresh(false);
/*     */     }
/* 168 */     return sLatestKey;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/AuthTokenKey.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */